package com.senriot.quickup.service.mapper

import com.senriot.cloud.common.IEntityMapper
import com.senriot.quickup.domain.Store
import com.senriot.quickup.service.dto.StoreDTO
import org.mapstruct.*

@Mapper(
    componentModel = "spring",
    uses = [AgencyMapper::class, CityMapper::class],
    nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
abstract class StoreMapper : IEntityMapper<StoreDTO, Store>
{
    fun fromId(id: Long?) = id?.let { Store().apply { this.id = it } }

    @Mappings(
        Mapping(target = "agencyId", source = "agency.id"),
        Mapping(target = "cityId", source = "city.id"),
        Mapping(target = "provinceId", source = "province.id"),
        Mapping(target = "districtId", source = "district.id"),
        Mapping(target = "cityName", source = "city.name"),
        Mapping(target = "provinceName", source = "province.name"),
        Mapping(target = "districtName", source = "district.name")
    )
    abstract override fun toDto(entity: Store): StoreDTO

    @Mappings(
        Mapping(target = "agency", source = "agencyId"),
        Mapping(target = "city", source = "cityId"),
        Mapping(target = "province", source = "provinceId"),
        Mapping(target = "district", source = "districtId")
    )
    abstract override fun toEntity(dto: StoreDTO): Store


    @Mappings(
        Mapping(target = "id", ignore = true),
        Mapping(target = "createdBy", ignore = true),
        Mapping(target = "createdDate", ignore = true),
        Mapping(target = "lastModifiedBy", ignore = true),
        Mapping(target = "lastModifiedDate", ignore = true),
        Mapping(target = "agency", source = "agencyId"),
        Mapping(target = "city", source = "cityId"),
        Mapping(target = "province", source = "provinceId"),
        Mapping(target = "district", source = "districtId")
    )
    abstract override fun updateEntity(dto: StoreDTO, @MappingTarget entity: Store): Store
}
